package com.example.springboot.web.common;

import com.example.springboot.web.common.ex.GlobalErrorCodeConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.Assert;


/**
 * @author lei yu
 * @date 2024/05/20
 */
@Data
@Schema(description = "公共返回")
public class R<T> {

    /**
     * 错误码
     */
    @Schema(description = "错误码", example = "0")
    private Integer code;

    /**
     * 错误提示，用户可阅读
     */
    @Schema(description = "错误提示", example = "成功")
    private String message;

    /**
     * 返回数据
     */
    @Schema(description = "返回数据", example = "{}")
    private T data;

    public static <T> R<T> ok() {
        R<T> result = new R<>();
        result.setCode(0).setMessage("成功");
        return result;
    }

    public static <T> R<T> ok(T data) {
        R<T> result = ok();
        result.setData(data);
        return result;
    }

    public static <T> R<T> error(Integer code, String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的！");
        R<T> result = new R<>();
        result.code = code;
        result.message = message;
        return result;
    }


}